﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WITD
{
    /// <summary>
    /// Interaction logic for TopologiesHelp.xaml
    /// <para></para><para></para>
    /// Displays the TFS Topologies.rtf file in a window.
    /// </summary>
    public partial class TopologiesHelp : Window
    {
        FileStream TopologyHelpFile = null;
        const String TFSTopologiesHelpFileName = "TFS Topologies.rtf";

        /// <summary>
        /// Constructor: Loads the rtf file from disk.
        /// </summary>
        public TopologiesHelp(Window parent)
        {
            InitializeComponent();
            Owner = parent;
            LoadFile(TFSTopologiesHelpFileName);
        }

        /// <summary>
        /// Loads the specified rtf file into the control.
        /// </summary>
        /// <param name="fileName"></param>
        private void LoadFile(String fileName)
        {
            String file = FindFile(fileName);

            if (!String.IsNullOrEmpty(file))
            {
                if (File.Exists(file))
                {
                    TopologyHelpFile = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                    using (TopologyHelpFile)
                    {
                        TopologyHelpFile.Seek(0, SeekOrigin.Begin);
                        TextRange tr = new TextRange(rtfView.Document.ContentStart, rtfView.Document.ContentEnd);
                        tr.Load(TopologyHelpFile, DataFormats.Rtf);
                    }

                }
            }
        }

        /// <summary>
        /// Closes this window and releases resources.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (TopologyHelpFile != null)
            {
                TopologyHelpFile.Close();
                TopologyHelpFile.Dispose();
            }
            this.Close();
        }

        /// <summary>
        /// Searches for the specified file starting in the app base folder.
        /// </summary>
        /// <param name="filename">The name of the file to search for.</param>
        /// <returns>The fully-qualified path name of the file, if found.</returns>
        private String FindFile(String filename)
        {
            String foundFile = String.Empty;
            IEnumerable<String> files;
            String baseFolder = AppDomain.CurrentDomain.BaseDirectory;
            files = System.IO.Directory.EnumerateFileSystemEntries(baseFolder, filename, SearchOption.AllDirectories);

            if (files.Count() > 0)
            {
                foreach (String file in files)
                {
                    if (file.Contains(filename))
                    {
                        foundFile = file;
                        break;
                    }
                }
            }

            return foundFile;
        }
    }
}
